﻿'******************************* File Header *********************************
'* File Name:    WmiCommand.vb
'* Project:      WmiConnection
'* Copyright (c) Freddy Kuehne
'*
'* Default implementation for a wmi command.
'* 
'* This source is subject to the GNU Library General Public License (LGPL).
'* See http://wmiconnection.codeplex.com/license.
'* All other rights reserved.
'*****************************************************************************

' TODO: Parameter-Support
' TODO: CommandBehaviour
''' <summary>
''' Implementation of WmiCommand.
''' </summary>
Public Class WmiCommand
  Inherits DbCommand
#Region "Variablen"
  ''' <summary>
  ''' ParameterCollection for this command.
  ''' </summary>
  Private m_Parameter As WmiParameterCollection
  ''' <summary>
  ''' CommandTimeout for this command.
  ''' </summary>
  Private m_CommandTimeout As Integer = 30
#End Region

#Region "Constructors"
  ''' <summary>
  ''' Creates a new WmiCommand.
  ''' </summary>
  ''' <param name="con">Connection to use when executing commands</param>
  ''' <exception cref="ArgumentNullException">Exception occurs, when an empty <paramref name="con"/> parameter is passed</exception>
  Friend Sub New(ByVal con As WmiConnection)
    If (con Is Nothing) Then Throw New ArgumentNullException("con")
    Connection = con
  End Sub
#End Region

#Region "Properties"
  ''' <summary>
  ''' Internal WMI-Object zu use for Queries.
  ''' </summary>
  Private ReadOnly Property Wmi As ManagementObjectSearcher
    Get
      Wmi = Nothing
      If (Connection IsNot Nothing) Then
        Wmi = DirectCast(Connection, WmiConnection).Wmi
      End If
    End Get
  End Property
#End Region

#Region "IDbCommand"
  ''' <summary>
  ''' <see cref="DbCommand.Cancel"/>
  ''' </summary>
  Public Overrides Sub Cancel()
  End Sub

  ''' <summary>
  ''' <see cref="DbCommand.CommandText"/>
  ''' </summary>
  Public Overrides Property CommandText As String
  ''' <summary>
  ''' <see cref="DbCommand.CommandType"/>
  ''' </summary>
  Public Overrides Property CommandType As CommandType
  ''' <summary>
  ''' <see cref="DbCommand.DbConnection"/>
  ''' </summary>
  Protected Overrides Property DbConnection As DbConnection
  ''' <summary>
  ''' <see cref="DbCommand.DbTransaction"/>
  ''' </summary>
  Protected Overrides Property DbTransaction As DbTransaction
  ''' <summary>
  ''' <see cref="DbCommand.DesignTimeVisible"/>
  ''' </summary>
  Public Overrides Property DesignTimeVisible As Boolean
  ''' <summary>
  ''' <see cref="DbCommand.UpdatedRowSource"/>
  ''' </summary>
  Public Overrides Property UpdatedRowSource As UpdateRowSource
  ''' <summary>
  ''' <see cref="DbCommand.CommandTimeout"/>
  ''' </summary>
  ''' <exception cref="ArgumentOutOfRangeException">Exception occurs, when a value &lt; 0 is set</exception>
  Public Overrides Property CommandTimeout As Integer
    Get
      CommandTimeout = m_CommandTimeout
    End Get
    Set(value As Integer)
      If (value < 0) Then Throw New ArgumentOutOfRangeException("value")
      m_CommandTimeout = value
    End Set
  End Property

  ''' <summary>
  ''' <see cref="DbCommand.CreateDbParameter"/>
  ''' </summary>
  Protected Overrides Function CreateDbParameter() As DbParameter
    CreateDbParameter = New WmiParameter
  End Function

  ''' <summary>
  ''' <see cref="DbCommand.ExecuteNonQuery"/>
  ''' </summary>
  ''' <returns>Always 0</returns>
  ''' <remarks>
  ''' ExecuteNonQuery is currently not supported!
  ''' This method always returns 0.
  ''' </remarks>
  Public Overrides Function ExecuteNonQuery() As Integer
    ExecuteNonQuery = 0
  End Function

  ''' <summary>
  ''' <see cref="DbCommand.ExecuteDbDataReader"/>
  ''' </summary>
  ''' <remarks>
  ''' CommandBehaviors are currently not supported and therefore ignored.
  ''' </remarks>
  Protected Overrides Function ExecuteDbDataReader(behavior As CommandBehavior) As DbDataReader
    Dim col As ManagementObjectCollection
    Wmi.Query = New WqlObjectQuery(CommandText)
    col = Wmi.Get()
    ExecuteDbDataReader = New WmiDataReader(col)
  End Function

  ''' <summary>
  ''' <see cref="DbCommand.ExecuteScalar"/>
  ''' </summary>
  Public Overrides Function ExecuteScalar() As Object
    Dim rd As IDataReader

    ExecuteScalar = Nothing
    rd = ExecuteReader()
    If (rd IsNot Nothing) Then
      If (rd.Read) Then
        ExecuteScalar = rd.Item(0)
      End If
    End If
  End Function

  ''' <summary>
  ''' <see cref="DbCommand.DbParameterCollection"/>
  ''' </summary>
  Protected Overrides ReadOnly Property DbParameterCollection As DbParameterCollection
    Get
      DbParameterCollection = m_Parameter
    End Get
  End Property

  ''' <summary>
  ''' <see cref="DbCommand.Prepare"/>
  ''' </summary>
  ''' <remarks>
  ''' Preparation of wmi-queries are not supported and will be ignored.
  ''' </remarks>
  Public Overrides Sub Prepare()
  End Sub
#End Region
End Class
